<template id="tree">
	<div>
		<el-tree v-bind:data="trees" ref="projectTree"
			v-bind:props="{label: 'label', children: 'children'}"
			node-key="key" v-bind:default-expanded-keys="defaultExpandedKeys"
			v-on:current-change="projectCurrentChange">
			<span class="custom-tree-node" slot-scope="{node, data}">
				<span v-text="data.label"></span>
				<span v-if="data.isHeadCompany"></span>
				<span v-else-if="data.isBranchCompany">
					<el-button type="text" size="small" v-on:click.stop="projectAddOpen(data)" >添加项目</el-button>
				</span>
				<span v-else-if="data.isHeadProject">
					<el-button type="text" size="small" v-on:click.stop="projectAddOpen(data)">添加子项目</el-button>
					<el-button type="text" size="small" v-on:click.stop="projectDetailOpen(data)">项目详情</el-button>
					<el-button type="text" size="small" v-on:click.stop="projectDelete(data)">删除</el-button>
				</span>
				<span v-else-if="data.isSubProject">
					<el-button type="text" size="small" v-on:click.stop="projectDetailOpen(data)">项目详情</el-button>
					<el-button type="text" size="small" v-on:click.stop="projectDelete(data)">删除</el-button>
				</span>
			</span>
		</el-tree>
	</div>
</template>

<script>
import cfg from './js/';
import store from '@/store';

export default {
	store: cfg.appStore,
	components: {
		
	},
	data() {
		return {
			defaultExpandedKeys: [],
		}
	},
	methods: {
		projectCurrentChange: function(data) {
			if(data.type != 'company') {
				this.$store.state.main.projectSelected = this.$store.state.main.projectMap[data.entityId];
			} 
			console.log(this.$refs.projectTree);
		},
		projectAddOpen: function(data){
			this.$emit('project-add-open', data);
		},
		projectDetailOpen: function(data){
			this.$emit('project-detail-open', data);
		},
		projectDelete: function(data){
			this.$emit('project-delete', data);
		},
		setProjectTree: function(key){
			this.$refs.projectTree.remove(key);
		},
		editProjecTree: function(key){
			this.$refs.projectTree.splice(this.$refs.projectTree.indexOf(key), 1, key);
		}
	},
	computed: {
		trees: function(){
			return this.$store.state.main.trees;
		}
	}
}
</script>